package com.slkj.shilixiaoyuanapp.ui.teacher.tool.association;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.SthdTypeAllEntity;
import com.slkj.shilixiaoyuanapp.entity.SthdTypeEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolCommonHistoryEntity;
import com.slkj.shilixiaoyuanapp.net.CommonResult;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.BaseFunctionCallBack;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.net.service.ToolService;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;

@ActivityInfo(layout = R.layout.activity_association, title = "社团活动")
public class AssociationActivity extends BaseActivity {
    @BindView(R.id.recycer)
    RecyclerView recycerView;
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    BaseQuickAdapter adapter;
    public List<SthdTypeAllEntity> datas = new ArrayList<>();
    PieChart mPieChart;
    List<SthdTypeEntity> typeData;
    @Override
    protected void init() {
        recycerView.setLayoutManager(new LinearLayoutManager(this));
        recycerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        adapter = new BaseQuickAdapter<SthdTypeAllEntity, BaseViewHolder>(R.layout.item_tool_association, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final SthdTypeAllEntity item) {
                helper.setText(R.id.tv_name,item.getCName())
                        .setText(R.id.tv_num,item.getCount()+"人")
                        .setText(R.id.tv_type,item.getCtName());
            }

        };
        adapter.setOnItemClickListener((adapter, view, position) -> {
        });
        recycerView.setAdapter(adapter);
        View view = getLayoutInflater().inflate(R.layout.head_association, (ViewGroup) recycerView.getParent(), false);
        mPieChart = view.findViewById(R.id.mPieChart);
        RadioGroup rag = view.findViewById(R.id.rag);

        rag.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.radio1 || i == R.id.radio2|| i == R.id.radio3) {
                    mPieChart.setCenterText(generateCenterSpannableText("33.33%\n3个社团",5));
                }
                else {
                    mPieChart.setCenterText(generateCenterSpannableText("0%\n3个社团",1));

                }
                mPieChart.invalidate();
            }
        });
        adapter.addHeaderView(view);
        getData();
        stateLayout.setOnReLoadListener(this::getData);
    }

    private void getData() {
        ToolService service = HttpHeper.get().toolService();
        service.getSthd()
                .flatMap(new BaseFunctionCallBack<List<SthdTypeEntity>, List<SthdTypeAllEntity>>() {
                    @Override
                    public ObservableSource<CommonResult<List<SthdTypeAllEntity>>> back(List<SthdTypeEntity> result) {
                        typeData = result;
                        return service.getSthdDetail();
                    }
                })
                .compose(getThread()).compose(bindToLifecycle())
                .subscribe(new CommonCallBack<List<SthdTypeAllEntity>>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<SthdTypeAllEntity> data) {
                        datas.addAll(data);
                        adapter.notifyDataSetChanged();
                        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
                        for(SthdTypeEntity entity :typeData){
                            entries.add(new PieEntry(entity.getCount()));
                            initPieChart(entries);
                        }

                    }
                });
    }

    private void initPieChart( ArrayList<PieEntry> entries) {
        mPieChart.getDescription().setEnabled(false);
        mPieChart.setCenterText(generateCenterSpannableText());
        mPieChart.setDrawHoleEnabled(true);
        mPieChart.setHoleColor(Color.WHITE);
        mPieChart.setTransparentCircleColor(Color.WHITE);
        mPieChart.setTransparentCircleAlpha(110);
        mPieChart.setHoleRadius(58f);
        mPieChart.setTransparentCircleRadius(61f);
        mPieChart.setDrawCenterText(true);
        mPieChart.getDescription().setEnabled(false);
        mPieChart.setUsePercentValues(false);
        mPieChart.setDrawEntryLabels(true);
        //设置数据
        setData(entries);
        // 输入标签样式
        mPieChart.setEntryLabelColor(Color.WHITE);
    }

    //设置中间文字
    private SpannableString generateCenterSpannableText() {
        SpannableString s = new SpannableString("33.33%\n3个社团");
        RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.5f);
        s.setSpan(sizeSpan01, 0, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return s;
    }

    //设置中间文字
    private SpannableString generateCenterSpannableText(String str,int pos) {
        SpannableString s = new SpannableString(str);
        RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.5f);
        s.setSpan(sizeSpan01, 0, pos, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return s;
    }
    //设置数据
    private void setData(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "");
        //数据和颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(Color.parseColor("#FF6953"));
        colors.add(Color.parseColor("#FF9D00"));
        colors.add(Color.parseColor("#FED504"));
        colors.add(Color.parseColor("#5DE2C8"));
        colors.add(Color.parseColor("#0087FC"));
        colors.add(Color.parseColor("#CD80FF"));
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setDrawValues(false);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        mPieChart.setData(data);
        mPieChart.highlightValues(null);
        //刷新
        mPieChart.invalidate();
    }
}
